package service.activity

import entity.Activity
import entity.ActivityType
import entity.User
import repository.ActivityRepository
import repository.ActivityRepositoryImpl
import repository.Page
import repository.Pageable

class ActivityServiceImpl : ActivityService {
    private val activityRepository: ActivityRepository = ActivityRepositoryImpl()

    override fun publishActivity(activityInfo: ActivityInfo, publisher: User, examiner: User): Int {
        var activity = Activity().apply {
            this.setInfo(activityInfo)
            this.publisher = publisher
            this.examiner = examiner
            this.checked = null
            this.publishTime = System.currentTimeMillis()
        }
        activity = activityRepository.save(activity)
        //保存一遍获取自增的Id
        activity.activityCode = generateActivityId(activity.id)
        val result = activityRepository.save(activity)
        return result.id
    }

    override fun changeActivityInfo(aCode: String, activityInfo: ActivityInfo) {
        val activity = activityRepository.findByActivityCode(aCode)
        activity.setInfo(activityInfo)
        activityRepository.save(activity)
    }

    override fun changeActivityInfo(aId: Int, activityInfo: ActivityInfo) {
        val activity = activityRepository.findById(aId).get()
        activity.setInfo(activityInfo)
        activityRepository.save(activity)
    }

    override fun deleteActivity(aCode: String) {
        val activity = activityRepository.findByActivityCode(aCode)
        activityRepository.delete(activity)
    }

    override fun deleteActivity(aId: Int) {
        val activity = activityRepository.findById(aId).get()
        activityRepository.delete(activity)
    }

    override fun examActivity(aCode: String, examined: Boolean) {
        val activity = activityRepository.findByActivityCode(aCode)
        activity.checked = examined
        activityRepository.save(activity)

    }

    override fun examActivity(aId: Int, examined: Boolean) {
        val activity = activityRepository.findById(aId).get()
        activity.checked = examined
        activityRepository.save(activity)
    }

    override fun getAllActivity(pageable: Pageable): Page<Activity>? {
        return activityRepository.findAll(pageable)
    }

    override fun getAllActivity(): List<Activity> {
        return activityRepository.findAll()
    }

    override fun findActivity(type: ActivityType, pageable: Pageable): Page<Activity> {
        return activityRepository.findByActivityTypeAndCheckedTrue(type, pageable)
    }

    override fun findActivity(type: ActivityType): List<Activity> {

        return activityRepository.findByActivityTypeAndCheckedTrue(type)
    }

    override fun findActivity(name: String): List<Activity> {
        return activityRepository.findByActivityNameIgnoreCaseContainingAndCheckedTrue(name)
    }

    override fun findActivity(name: String, pageable: Pageable): Page<Activity> {
        return activityRepository.findByActivityNameIgnoreCaseContainingAndCheckedTrue(name, pageable)
    }

    override fun findActivityByCode(aCode: String): Activity {
        return activityRepository.findByActivityCode(aCode)
    }

    override fun findActivity(id: Int): Activity? {

        return activityRepository.findById(id).let {
            if (it.isPresent) {
                it.get()
            } else {
                null
            }
        }
    }

    override fun findActivity(checked: Boolean): List<Activity> {
        return if (checked) {
            activityRepository.findByCheckedTrue()
        } else {
            activityRepository.findByCheckedFalse()
        }
    }

    override fun findActivity(checked: Boolean, pageable: Pageable): Page<Activity> {
        return if (checked) {
            activityRepository.findByCheckedTrue(pageable)
        } else {
            activityRepository.findByCheckedFalse(pageable)
        }
    }

    override fun findActivityByPublisher(publisher: User, checked: Boolean?): List<Activity> =
            when (checked) {
                null -> {
                    activityRepository.findByPublisherAndCheckedIsNull(publisher)
                }
                true -> {
                    activityRepository.findByPublisherAndCheckedTrue(publisher)
                }
                false -> {
                    activityRepository.findByPublisherAndCheckedFalse(publisher)
                }
            }

    override fun findActivityByPublisher(publisher: User, pageable: Pageable): Page<Activity> {
        return activityRepository.findByPublisher(publisher,pageable)
    }

    override fun findActivityByPublisher(publisher: User, checked: Boolean?, pageable: Pageable): Page<Activity> =
            when (checked) {
                null -> {
                    activityRepository.findByPublisherAndCheckedIsNull(publisher, pageable)
                }
                true -> {
                    activityRepository.findByPublisherAndCheckedTrue(publisher, pageable)
                }
                false -> {
                    activityRepository.findByPublisherAndCheckedFalse(publisher, pageable)
                }
            }

    override fun findActivityByExaminer(examiner: User, checked: Boolean?): List<Activity> =
            when (checked) {
                null -> {
                    activityRepository.findByExaminerAndCheckedIsNull(examiner)
                }
                true -> {
                    activityRepository.findByExaminerAndCheckedTrue(examiner)
                }
                false -> {
                    activityRepository.findByExaminerAndCheckedFalse(examiner)
                }
            }

    override fun findActivityByExaminer(examiner: User, checked: Boolean?, pageable: Pageable): Page<Activity> =
            when (checked) {
                null -> {
                    activityRepository.findByExaminerAndCheckedIsNull(examiner, pageable)
                }
                true -> {
                    activityRepository.findByExaminerAndCheckedTrue(examiner, pageable)
                }
                false -> {
                    activityRepository.findByExaminerAndCheckedFalse(examiner, pageable)
                }
            }


    private fun generateActivityId(id: Int): String {
        return id.toString()
    }
}